package StudyCode.summary.mycollection;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class AppendOneObjectToAnArray {
	public static void main(String[] argv) {
		Object[] array = new String[]{"1", "abc", "level", null,
				"www.tah1986.com", "asdf 123"};
		Object toAppend = "www.tah1986.com";
		System.out.println(java.util.Arrays.toString(appendToArray(array,
				toAppend)));
	}

	public static Object[] appendToArray(Object[] array, Object toAppend) {
		return appendToArray(array, new Object[]{toAppend});
	}

	public static Object[] appendToArray(Object[] array, Object[] toAppend) {
		Class c;
		if (array.length > 0)
			c = array[0].getClass();
		else
			c = toAppend[0].getClass();

		Object[] tmp = (Object[]) java.lang.reflect.Array.newInstance(c,
				array.length + toAppend.length);
		System.arraycopy(array, 0, tmp, 0, array.length);
		System.arraycopy(toAppend, 0, tmp, array.length, toAppend.length);

		return tmp;
	}
}
